% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Replication "Deconstructing the Yield Curve"
% Crump and Gospodinov (2024)
% Date: 26-JUL-2024
% Function for simulating bond data for simulations
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
function [simfwd,simPredX] = simulateData(data2,data,p,sp,design)
    
if design < 3
    simV = randn(sp.T-1,p.K)*chol(data2.SigHat);    
    simX = buildVAR(data2.muHat, data2.PhiHat, simV', data2.X1(1,2:end)')';
    simX = [data2.X(1,:); simX];
    if design == 2
        simPredV = randn(sp.T-1,size(data.pred,2))*chol(data2.pred.SigHat);
        simPredX = buildVAR(data2.pred.muHat, data2.pred.PhiHat, simPredV', data.pred(1,:)')';
        simPredX = [data.pred(1,:); simPredX];
    end
elseif design == 3
    simV = randn(sp.T-2,p.K)*chol(data2.SigHat);    
    simX = buildVAR([data2.muHat; zeros(p.K,1)], data2.PhiHat, [simV'; zeros(p.K,sp.T-2)], data2.X12(1,2:end)')';
    simX = [data2.X(1:2,:); simX(:,1:p.K)];
end

simfwd = [ones(sp.T,1) simX]*data2.loadHat + randn(sp.T,numel(p.yldMats))*data2.SigEHatSqrt;

end